/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


var defaultLabelColor = "black";
var defaultLabelErrColor = "red";
var minPassLength = 6;
var maxPassLength = 15;

var emailRegex = new RegExp ("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");


$("#registerPage #errorTextLabelReg").css ('color', defaultLabelErrColor);
$("#createSalePage tr.locationSetRow").hide();

$("#registerPage #user_register_button").click(function () 
{
    var success = true;
    if ($('#registerPage #first_name').val() == "") 
    {
	$('#registerPage #first_name_label').css ('color', defaultLabelErrColor);
	success = false;
    }     
    else
    {	    
	$('#registerPage #first_name_label').css ('color', defaultLabelColor);
    }
    
        
    if ($('#registerPage #last_name').val() == "") 
    {
	$('#registerPage #last_name_label').css ('color', defaultLabelErrColor);
	success = false;
    } 	     
    else
    {	    
	$('#registerPage #last_name_label').css ('color', defaultLabelColor);
    }
    
    if ($('#registerPage #password').val() == "") 
    {
	$('#registerPage #password_label').css ('color', defaultLabelErrColor);
	success = false;
    } 	     
    else
    {	    
	$('#registerPage #password_label').css ('color', defaultLabelColor);
    }
    
    if ($('#registerPage #password2').val() == "") 
    {
	$('#registerPage #password2_label').css ('color', defaultLabelErrColor);
	success = false;
    } 	     
    else
    {	    
	$('#registerPage #password2_label').css ('color', defaultLabelColor);   
    }
    
    if ( $('#registerPage #email').val() == "") 
    {
	$('#registerPage #email_label').css ('color', defaultLabelErrColor);
	success = false;
    } 	     
    else
    {	    
	$('#registerPage #email_label').css ('color', defaultLabelColor);    
    }
    
    
    if ( $('#registerPage #country_dropdown').val() == "") 
    {
	$('#registerPage #country_label').css ('color', defaultLabelErrColor);
	success = false;
    } 	     
    else
    {	    
	$('#registerPage #country_label').css ('color', defaultLabelColor);    
    }
    
    if ( $('#registerPage #region_dropdown').val() == "") 
    {
	$('#registerPage #region_label').css ('color', defaultLabelErrColor);
	success = false;
    } 	     
    else
    {	    
	$('#registerPage #region_label').css ('color', defaultLabelColor);    
    }
    
    if ( $('#registerPage #city_dropdown').val() == "") 
    {
	$('#registerPage #city_label').css ('color', defaultLabelErrColor);
	success = false;
    } 	     
    else
    {	    
	$('#registerPage #city_label').css ('color', defaultLabelColor);    
    }

    if (!success)
    {
	$('#registerPage #errorTextLabelReg').text("Please enter the highlighted values");
	return;	    
    }
    
    if ($('#registerPage #password').val() != $('#registerPage #password2').val())
    {
	$('#registerPage #password_label').css ('color', defaultLabelErrColor);	
	$('#registerPage #password2_label').css ('color', defaultLabelErrColor);
	
	$('#registerPage #errorTextLabelReg').text("The passwords do not match");
	return;
    }
    
    if ($('#registerPage #password').val().length < minPassLength || $('#registerPage #password').val().length > maxPassLength)
    {
	$('#registerPage #password_label').css ('color', defaultLabelErrColor);	
	$('#registerPage #password2_label').css ('color', defaultLabelErrColor);
	
	$('#registerPage #errorTextLabelReg').text("Passwords must be between " + minPassLength + " and " + maxPassLength + " characters long");
	return;
    }
    
    
    
    if (!emailRegex.test($('#registerPage #email').val()))
    {
	$('#registerPage #email_label').css ('color', defaultLabelErrColor);	
	
	$('#registerPage #errorTextLabelReg').text("Invalid email address");
	return;
	    
    }
    
    $('#registerPage #errorTextLabelReg').text("");
    
    $.post("DoRegister", {
	"email" : $('#registerPage #email').val(), 
	"firstName" : $('#registerPage #first_name').val(), 
	"lastName" : $('#registerPage #last_name').val(), 
	"password" : $('#registerPage #password').val(), 
	"country" : $('#registerPage #country_dropdown').val(), 
	"region" : $('#registerPage #region_dropdown').val(), 
	"city" : $('#registerPage #city_dropdown').val()
    }, function(data) 

    {
	    var jsonObj = jQuery.parseJSON( data );
	
	    if (jsonObj.success)
	    {		
		$('#registerPage #errorTextLabelReg').text("User successfully registered.");
	    }
	    else
	    {
		    
		$('#registerPage #errorTextLabelReg').text(jsonObj.reason);
	    }
	});
});


$("#createSalePage input.locationEnterTypeRow").change(function (i)
{
    
    $("#createSalePage tr.locationSetRow").toggle();
    
});

$("#createSalePage #country_dropdown").change(function (i) 
{
    $("#createSalePage #region_dropdown").html($('<option></option>').html(""));
    $("#createSalePage #city_dropdown").html($('<option></option>').html(""));
    
    $.post("MapCountryToRegions", {
	"country" : $('#createSalePage #country_dropdown').val()
    }, function(data) 

    {
	    
	    if (data != "null")
	    {		
		var regionList = data.split(",");
		
		for (var i in regionList) 
		{
		    $("#createSalePage #region_dropdown").append($('<option></option>').html(regionList[i]));
		}
	    }
	
	});
});

$("#createSalePage #region_dropdown").change(function (i) 
{
    $("#createSalePage #city_dropdown").html($('<option></option>').html(""));
    
    $.post("MapRegionToCities", {
	"country" : $('#createSalePage #country_dropdown').val(), 
	"region" : $('#createSalePage #region_dropdown').val()
    }, function(data) 

    {
	    if (data != "null")
	    {
		
		var cityList = data.split(",");
		
		for (var i in cityList) 
		{
		    $("#createSalePage #city_dropdown").append($('<option></option>').html(cityList[i]));
		}
	    }
	
	});
});

$("#registerPage #country_dropdown").change(function (i) 
{
    $("#registerPage #region_dropdown").html($('<option></option>').html(""));
    $("#registerPage #city_dropdown").html($('<option></option>').html(""));
    
    $.post("MapCountryToRegions", {
	"country" : $('#registerPage #country_dropdown').val()
    }, function(data) 

    {
	    
	    if (data != "null")
	    {		
		var regionList = data.split(",");
		
		for (var i in regionList) 
		{
		    $("#registerPage #region_dropdown").append($('<option></option>').html(regionList[i]));
		}
	    }
	
	});
});

$("#registerPage #region_dropdown").change(function (i) 
{
    $("#registerPage #city_dropdown").html($('<option></option>').html(""));
    
    $.post("MapRegionToCities", {
	"country" : $('#registerPage #country_dropdown').val(), 
	"region" : $('#registerPage #region_dropdown').val()
    }, function(data) 

    {
	    if (data != "null")
	    {
		
		var cityList = data.split(",");
		
		for (var i in cityList) 
		{
		    $("#registerPage #city_dropdown").append($('<option></option>').html(cityList[i]));
		}
	    }
	
	});
});

$("#createSalePage #create_sale_button").click(function (i)
{
    var saleTitle = $("#createSalePage #sale_title").val();
    var saleDesc = $("#createSalePage #sale_desc").val();
    var salePrice = $("#createSalePage #price").val();
    
    var locationMode;
    
    if ($("#createSalePage #myCityLocation:checked").val())
    {
	locationMode = "1";
    }
    else
    {
	locationMode = "2"
    }
    
    var saleCatgory = $("#createSalePage #sale_category_dropdown").val();
    var postVals =  {
	"title" : saleTitle, 
	"description" : saleDesc,
	"price" : salePrice, 
	"sell_in" : locationMode,
	"category" : saleCatgory
    };
    
    if (locationMode == "2")
    {
	postVals ["country"] = $("#createSalePage #country_dropdown").val();
	postVals ["region"] = $("#createSalePage #region_dropdown").val();
	postVals ["city"] = $("#createSalePage #city_dropdown").val();

    }
    
    $.post("DoCreateSaleEntry", postVals, function(data) 

    {
	    var jsonObj = jQuery.parseJSON( data );
	
	    if (jsonObj.success)
	    {		
		$('#createSalePage #errorTextLabelReg').text("Sale successfully created.");
	    }
	    else
	    {
		    
		$('#createSalePage #errorTextLabelReg').text(jsonObj.reason);
	    }
	
	});
}
);
